package com.teach;

import javax.servlet.http.HttpSession;

import com.teach.dto.LoginUser;

import cn.hutool.core.util.StrUtil;

/**
 * <pre>
 * TODO : 常量类
 * ClassName : com.teach.Const
 * Author : J.L.Zhou
 * Date : 2020-10-25 10:27
 * Version : V1.0
 * Copyright 2020 jlzhou.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes</pre>
 */
public class Const {

	private Const() {}
	
	/**
	 * 登录的用户信息在会话中保存的属性名
	 */
	public final static String SESSION_LOGIN_USER = "user";
	
	/**
	 * 获取session中保存的登录用户信息
	 * @param session
	 * @return
	 */
	public final static LoginUser getLoginUser(HttpSession session) {
		return (LoginUser)session.getAttribute(SESSION_LOGIN_USER);
	}
	
	/**
	 * 网页控制器的统一扩展名
	 */
	public final static String WEB_EXT = ".do";
	
	
    /**
     * - 验证码在session中的属性名
     */
    public static final String KAPTCHA_SESSION_NAME = "KAPTCHA_SESSION_NAME";
    
    /**
     * - 校验验证码
     * @param session
     * @param code
     * @throws RuntimeException:验证码不能为空|验证码错误
     */
    public static void checkKaptcha(HttpSession session,String code) {
        String code1 = (String)session.getAttribute(KAPTCHA_SESSION_NAME);
        if(StrUtil.hasBlank(code1,code)) {
            throw new RuntimeException("验证码不能为空");
        }
        if(!code1.equalsIgnoreCase(code)) {
            throw new RuntimeException("验证码错误");
        }
    }
}
